/*    file: main.cpp
 *    desc:
 * 
 * created: 2016-04-06
 *  author: chuanjiang.zh@qq.com
 * company: 
 */

#include <stdio.h>
#include <iostream>

#include "BasicType.h"
#include "SharedPtr.h"
#include "ImageWatcher.h"
#include "BasicMacro.h"
#include "TStringUtil.h"
#include <assert.h>
#include <errno.h>
#include "DateTime.h"
#include "Path.h"
#include "TSafeStr.h"

class Application
{
public:
	Application()
	{
		ImageWatcher::startup();
	}

	~Application()
	{
		ImageWatcher::cleanup();
	}

};



void testQuery(ImageWatcher& watcher)
{
	printf("query record ... \n");

	time_t fromTime = 0;
	time_t toTime   = 0;// fromTime + 24 * 60 * 60;

	EventRecord records[128];
	memset(records, 0, sizeof(records));
	int count = watcher.findEvent("", "", 0, fromTime, toTime, records, ARRAY_SIZE(records));
	for (int i = 0; i < count; i++)
	{
		printf("record. id:%d, company:%s, station:%s, num:%d, filepath: %s\n",
			records[i].id, records[i].company, 
			records[i].station, records[i].num, records[i].filepath);
	}

	printf("\n");
}



int main(int argc, char** argv)
{
	Application app;

	std::string workdir = comn::Path::getWorkDir();
	
	std::string imageDir = comn::Path::join(workdir, "images");
	std::string imageDir2 = comn::Path::join(workdir, "images2");
		
	ImageWatcher& watcher = ImageWatcher::instance();

	watcher.addImageDir(imageDir.c_str(), "/images/1");
	watcher.addImageDir(imageDir2.c_str(), "/images/2");

	int rc = watcher.open(8800, "");
	if (rc != 0)
	{
		return 0;
	}
	
	testQuery(watcher);

	while (true)
	{
		std::cout << "$>>";
		std::string line;
		std::getline(std::cin, line);
		if (line == "q" || line == "quit")
		{
			break;
		}
		else if (line == "image")
		{
			ImageRecord record;
			memset(&record, 0, sizeof(record));

			comn::copyStr(record.company, "admin");
			comn::copyStr(record.station, "admin");
			record.num = 1;
			std::string filepath = comn::Path::join(imageDir2, "group.png");
			comn::copyStr(record.filepath, filepath);
			record.created = time(NULL);
			watcher.addImage(record);
		}
		else if (line == "event")
		{
			EventRecord record;
			memset(&record, 0, sizeof(record));
			
			record.type = EVENT_RECORD_ALARM;
			record.capacity = 10;
			record.peoples = 2;
			comn::copyStr(record.company, "demo");
			comn::copyStr(record.station, "desk");
			record.num = 2;
			std::string filepath = comn::Path::join(imageDir, "admin.png");
			comn::copyStr(record.filepath, filepath);
			record.created = time(NULL);
			watcher.addEvent(record);
		}
	}

	watcher.close();

	return 0;
}




